package com.ifast.sys.base.controller;

import com.ifast.common.annotation.Log;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.base.domain.Tree;
import com.ifast.common.utils.Result;
import com.ifast.sys.base.domain.MenuDO;
import com.ifast.sys.base.service.MenuService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre>
 * </pre>
 * 
 * <small> 2018年3月23日 | Aron</small>
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends AdminBaseController {
	String      prefix = "sys/menu";
	@Autowired
	MenuService menuService;

	@RequiresPermissions("sys:menu:menu")
	@GetMapping()
	String menu(Model model) {
		return prefix + "/menu";
	}

	@RequiresPermissions("sys:menu:menu")
	@RequestMapping("/list")
	@ResponseBody
	List<MenuDO> list() {
		// List<MenuDO> menus = menuService.selectList(null);
		List<MenuDO> menus = menuService.list(null);
		return menus;
	}

	@RequiresPermissions("sys:menu:add")
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			// model.addAttribute("pName", menuService.selectById(pId).getName());
			model.addAttribute("pName", menuService.getById(pId).getName());
		}
		return prefix + "/add";
	}

	@RequiresPermissions("sys:menu:edit")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		// MenuDO mdo = menuService.selectById(id);
		MenuDO mdo = menuService.getById(id);
		Long   pId = mdo.getParentId();
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			// model.addAttribute("pName", menuService.selectById(pId).getName());
			model.addAttribute("pName", menuService.getById(pId).getName());
		}
		model.addAttribute("menu", mdo);
		return prefix + "/edit";
	}

	@Log("保存菜单")
	@RequiresPermissions("sys:menu:add")
	@PostMapping("/save")
	@ResponseBody
	Result<String> save(MenuDO menu) {
		// menuService.insert(menu);
		menuService.save(menu);
		return Result.ok();
	}

	@Log("更新菜单")
	@RequiresPermissions("sys:menu:edit")
	@PostMapping("/update")
	@ResponseBody
	Result<String> update(MenuDO menu) {
		menuService.updateById(menu);
		return Result.ok();
	}

	@Log("删除菜单")
	@RequiresPermissions("sys:menu:remove")
	@PostMapping("/remove")
	@ResponseBody
	Result<String> remove(Long id) {
		// menuService.deleteById(id);
		menuService.removeById(id);
		return Result.ok();
	}

	@Log("查询菜单树形数据")
	@GetMapping("/tree")
	@ResponseBody
	Tree<MenuDO> tree() {
		Tree<MenuDO> tree = new Tree<MenuDO>();
		tree = menuService.getTree();
		return tree;
	}

	@GetMapping("/tree/{roleId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
		Tree<MenuDO> tree = new Tree<MenuDO>();
		tree = menuService.getTree(roleId);
		return tree;
	}
}
